#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/un.h>

// 用法：./server
int main(int argc, char const* argv[])
{ 
    
    int server_socket = socket(AF_UNIX,SOCK_STREAM,0);
    if (server_socket==-1)
    {
        perror("socket err\n");
        return -1;
    }
    
    struct sockaddr_un addr;
    addr.sun_family = AF_UNIX;
    strcpy(addr.sun_path,"unix_sock");

    remove("unix_sock");

    if(bind(server_socket,(struct sockaddr*)&addr,sizeof(addr))==-1)
    {
        perror("bind err\n");
        return -1;
    }
    if (listen(server_socket,5)==-1)
    {
        perror("listen err\n");
        return -1;
    }
    printf("wait client connect\n");
    int client_socket = -1;
    client_socket = accept(server_socket,NULL,NULL);
    if (client_socket < 0)
    {
        perror("accept err\n");
        return -1;
    }

    char buf[64]={'\0'};     
    while (1)
    {
        int ret =0;
        ret= recv(client_socket,buf,64,0);
        if (ret<0)
        {
            perror("recv err\n");
            return -1;
        }
        else if (ret >0)
        {
            printf("%s\n",buf);
        }
        else
        {
            printf("peer quit\n");
        }            
        
    }
    close(client_socket);
    close(server_socket);
    
    return 0;
}